package code;

import java.util.Scanner;
/**
 * 
 * @author
 * 
 * This class contains a main method that provides a menu with the options 
 * to interact with the program and update the store inventory information.
 */
public class DepartmentStore {
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		ItemList itemList = new ItemList();
		while(true){
			printMenu();//print the menu
			System.out.print("Please select an option: ");//ask for user to select an option
			String cmd = scanner.nextLine();
			if(cmd.equalsIgnoreCase("C")){
				System.out.println();
				itemList.cleanStore();
			}
			if(cmd.equalsIgnoreCase("I")){
				try {
					System.out.print("Enter the name: ");
					String name = scanner.nextLine();
					System.out.print("Enter the RFID: ");
					String rfidTag = scanner.nextLine();
					System.out.print("Enter the original location: ");
					String initPosition = scanner.nextLine();
					System.out.print("Enter the price: ");
					double price = Double.parseDouble(scanner.nextLine());
					itemList.insertInfo(name, rfidTag, price, initPosition);//insert the iteminfo order by RFID
				} catch (RfidTagNumberLengthException e) {
					System.out.println(e.getMessage());
				} catch (RfidTagNumberNotHexException e) {
					System.out.println(e.getMessage());
				} catch (OriginalLocationLengthException e) {
					System.out.println(e.getMessage());
				} catch (OriginalLocationFormatException e) {
					System.out.println(e.getMessage());
				} catch (NumberFormatException e) {
					System.out.println("Price format is wrong!");
				} catch (PriceNegativeException e) {
					System.out.println(e.getMessage());
				}
			}
			if(cmd.equalsIgnoreCase("L")){
				System.out.print("Enter the location: ");
				String location = scanner.nextLine();
				System.out.println();
				itemList.printByLocation(location);//print all the info whose locations are equal to the location user inputs
			}
			if(cmd.equalsIgnoreCase("M")){
				System.out.print("Enter the RFID: ");
				String rfidTag = scanner.nextLine();
				System.out.print("Enter the original location: ");
				String origin = scanner.nextLine();
				System.out.print("Enter the new location: ");
				String current = scanner.nextLine();
				try {
					itemList.moveItem(rfidTag, origin, current);//move the item from origin to current
				} catch (LocationFormatException e) {
					System.out.println(e.getMessage());
				}
			}
			if(cmd.equalsIgnoreCase("O")){
				System.out.print("Enter the cart number: ");
				String cartNumber = scanner.nextLine();
				System.out.println();
				try {
					double total = itemList.checkOut(cartNumber);//checkout all products in the cart with the number cartNumber
					System.out.println("The total cost for all merchandise in cart "+cartNumber.substring(1)+" was $"+String.format("%.2f", total));
				} catch (LocationFormatException e) {
					System.out.println(e.getMessage());
				}
			}
			if(cmd.equalsIgnoreCase("P")){
				System.out.println();
				itemList.printAll();//print all the info
			}
			if(cmd.equalsIgnoreCase("R")){
				System.out.print("Enter the RFID: ");
				String rfidTag = scanner.nextLine();
				itemList.printByRfidTag(rfidTag);//print all the info with rfidTag
			}
			if(cmd.equalsIgnoreCase("U")){
				System.out.println();
				itemList.removeAllPurchased();//remove all the products whose current location are out
			}
			if(cmd.equalsIgnoreCase("Q")){
				System.out.println();
				System.out.println("Goodbye!");
				scanner.close();//close the input stream
				System.exit(0);//exit
			}
		}
	}
	public static void printMenu(){
		System.out.println();
		System.out.println("Welcome!");
		System.out.println();
		System.out.println("C - Clean store");
		System.out.println("I - Insert an item into the list");
	    System.out.println("L - List by location"); 
	    System.out.println("M - Move an item in the store");
	    System.out.println("O - Checkout");
	    System.out.println("P - Print all items in store");
	    System.out.println("R - Print by RFID tag number"); 
	    System.out.println("U - Update inventory system");
	    System.out.println("Q - Exit the program");
	    System.out.println();
	}
}
